
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.push.tablename;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import dbmanager.DBManager;

import pool.bean.Transaction_Bean;


public class Transaction extends IntegrateSubPool
{
	
	private String masterTable= "transaction";
	private String transPlTable = "trans_property_details";
	
	private Transaction_Bean trans;	
	private StringBuffer strbfr = new StringBuffer();
	String fieldname="";
	//LinkedList<Object> fieldNameList = new LinkedList<Object>();
	
	/**Getting recent object values from pool table.*/
	private void generatePoolHash()
	{		
		//add all details got from transaction done
		getTransHashtable(recentTableObject);
		String []transArray = strbfr.toString().split("\n");
		String []columnArray = transArray[0].split("\t");
		
		for(int i=1;i<transArray.length;i++)
		{
			String []valueArray = transArray[i].split("\t");
			for(int j=0;j<valueArray.length;j++)
			{
				poolHash.put(columnArray[j],valueArray[j]);
			}
			poolVector.add(poolHash);
		}	
	}
	
	public Hashtable<Object, Object> getIntegratePoolHash() 
	{
		generatePoolHash();
		return poolHash;
	}
	
	
	public Vector<Object> getPropertyVector() 
	{		
		Enumeration<Object> poolIte = fieldHash.keys();
		while(poolIte.hasMoreElements())
		{
			String key = poolIte.nextElement().toString();
			String value = fieldHash.get(key).toString();
			Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
			hash.put("pid", key);
			hash.put("pv", value);
			propertyVect.add(hash);
			//FLAG = true;
		}		
		return propertyVect;
	}

	
	public String insert()
	{
		return masterObj;
	}

	
	public void setPropertyVector(Vector<Object> vector)
	{
		propertyVect.addAll(vector);	
	}
	
	
	public String getID(Hashtable<Object,Object> hashtable) 
	{		
		
		ResultSet rsPoolValue;
		String tid;
		try 
		{
			
			String masterid2 = hashtable.get("masterid").toString();			
			
			rsPoolValue = DBManager.getSelect("SELECT * FROM "+masterTable+"  WHERE id = " + masterid2 );
			rsPoolValue.next();
			// get the object from the property_details where id = id_var and type = tableobj then call to insert master and generatePoolHash() function
			tid=masterid2;	
			rsPoolValue.close();
		return tid;				
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return null;
	}

	/*// It will returns the field from fieldObject for the specific TableObject.
//	private String getFieldname(String master, String key) 
	private LinkedList<Object> getFieldname()
	{
		ResultSet rsPoolValue;
		rsPoolValue = dbsql.getSelect("SELECT * FROM "+masterTable+"  WHERE id = " + master);
		rsPoolValue.next();
		fieldname=master;			
		
		*//**SELECT p4.pv FROM property_details p1 JOIN (property_details p2 JOIN (property_details p3 JOIN property_details p4 ON (p3.mid = p4.mid)) ON (p2.pv = p3.mid)) ON (p1.pv = p2.mid)
		where p1.pid = 26 and p1.mid = '9145' and p2.pid = '27' and p3.pid = '8290' and p3.pv = '9008' and p4.pid = '27'*//*
		
		String fieldListId = gin.getId("fieldlist");
		String columnId = gin.getId("column");
		String fieldId = gin.getId("field");
		
		if(uniqueKey == "")
		{
			uniqueKey = key;
		}
		
		
		for (int i = 0; i < uniqueKeyList.size(); i++) 
		{
			String uniqueKey = null; 
			uniqueKey = uniqueKeyList.get(i).toString();		
		
			String query = "SELECT p4.pv as field FROM property_details p1 JOIN (property_details p2 JOIN (property_details p3 JOIN property_details p4 ON (p3.mid = p4.mid)) ON (p2.pv = p3.mid)) ON (p1.pv = p2.mid)"+
							" WHERE p1.pid = " + fieldListId + " and p1.mid = " + tableObj + " and p2.pid = " + fieldId + " and p3.pid = " + columnId+
							" and p3.pv = " + uniqueKey + " and p4.pid = " + fieldId;
			
			rsPoolValue = dbsql.getSelect(query);
			try {
				rsPoolValue.next();
				fieldNameList.add(rsPoolValue.getString("field"));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			catch (Exception e) { }
		}
		return fieldNameList;
	}*/

	/** It will returns the value of fieldname from database.**/
	public String getFieldValue(String master, String key) 
	{	
		String fieldvalue;
		String query = "select pv from "+transPlTable+" where tid="+master+" and pid="+key; 
		ResultSet rsPoolValue = DBManager.getSelect(query);
		try
		{
			rsPoolValue.next();				
			fieldvalue = rsPoolValue.getString(1);
			if(fieldvalue.equals("0"))
				fieldvalue = rsPoolValue.getString("vt");
			else
				fieldvalue = gin.getItem(fieldvalue);
			
			rsPoolValue.close();
			return fieldvalue;			
		} catch (SQLException e) {
			fieldvalue=null;
		}				
		return fieldvalue;
	}
	
	public String update() {
		return masterObj;
	}
	
	
	//this function retrieves details related to particular id
	private  StringBuffer getTransHashtable(String id)
	{
		Hashtable<Object,Object> ViewHashtable = new Hashtable<Object, Object>();
		Hashtable<Object,Object> PropertyViewHashtable = new Hashtable<Object, Object>();
		
		request = masterTable;
		setRequestParam();
		
		ViewHashtable.put("id",id);
		PropertyViewHashtable.put("tid",id);
		
		 trans= new Transaction_Bean();
		trans.setReqParam(rqp);
		trans.setProcess("view");		
		trans.setViewHashtable(ViewHashtable);		
		trans.setPropertyViewHashtable(PropertyViewHashtable);
		
		strbfr.append(trans.view());		
		
		return strbfr;
	}
	
	
	
	
	public Hashtable<Object, Object> getIntegrateOldPropertyHash() {
		generatePoolHash();
		return poolHash;
	}

	
	public Vector<Object> getIntegrateOldPropertyVector() {
		getPropertyVector();
		return propertyVect;
	}


	
	public String delete() {
		return masterObj;
	}

	
	public void setOldPropertyVector(Vector<Object> vector) {
		// TODO Auto-generated method stub
		
	}

	public Hashtable<Object, Object> getUniqueKeyPoolHash() {
		
		//getFieldname(master, uniqueKey); // It will returns the field from fieldObject for the specific TableObject.
		getFieldname();
			
		for(int i = 0; i < fieldNameList.size(); i++)
		{
			String query = "SELECT pv as uniquekeyvalue,vt FROM trans_property_details  WHERE tid = " + recentTableObject + " AND pid = " + fieldNameList.get(i).toString();
			try {
				ResultSet rs = DBManager.getSelect(query);
				rs.next();
				String uniquekeyvalue = rs.getString("uniquekeyvalue");
				if(uniquekeyvalue.equals("0"))
					uniquekeyvalue = rs.getString("vt");
				else 
					uniquekeyvalue = gin.getItem(uniquekeyvalue);
				rs.close();
				//poolHash.put(gin.getItem(uniqueKey), uniquekeyvalue);
				poolHash.put(gin.getItem(fieldNameList.get(i).toString()), uniquekeyvalue);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return poolHash;
		//return new Hashtable<Object, Object>();
	}
}
